package com.hc.testaudio.deletea.test1.chart;

import android.content.Context;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.data.Entry;

import java.util.ArrayList;
import java.util.List;

/**
 * 线图
 */
public class LineChartUtil {
    private LineChart mLineChart;

    public LineChartUtil(LineChart barChart, Context context) {
        this.mLineChart = barChart;
        mLineChart.setNoDataText("");
//        mLineChart.setOnChartValueSelectedListener(this);//设置点击事件

        mLineChart.getDescription().setEnabled(false);

        // if more than 60 entries are displayed in the chart, no values will be
        // 设置启用setDrawValues（）时，图表上最大可见绘制值的数量仅处于活动状态*
        mLineChart.setMaxVisibleValueCount(60);

        // scaling can now only be done on x- and y-axis separately
        mLineChart.setPinchZoom(false);
        mLineChart.setDrawGridBackground(false);//是否绘制网格背景

        //底部x轴配置
        XAxis xAxis = mLineChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置x轴标签的位置
//        xAxis.setTypeface(tfLight);//设置特定字体
        xAxis.setDrawGridLines(false);//x轴是否绘制网格线
        xAxis.setGranularity(1f); // x轴最小间距
//        xAxis.setLabelCount(7);
        xAxis.setDrawAxisLine(false);//是否绘制延轴线
//        xAxis.setValueFormatter(ValueFormatter);//可以用于自定义x轴格式

        //左侧y轴配置
        YAxis leftAxis = mLineChart.getAxisLeft();
        leftAxis.setLabelCount(8, false);
//        leftAxis.setValueFormatter(custom);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(15f);//将顶部轴空间设置为整体的百分比。默认10f（即10%）
//        leftAxis.setAxisMinimum(0f); //通过调用此方法，先前设置的任何自定义最小值将被重置，并自动完成计算。 this replaces setStartAtZero(true)
        mLineChart.setMarker(new ChartMarker(context));
    }

    public void add(List<Float> yDataList) {
        List<Entry> dataEntry = new ArrayList<>();
        int length = yDataList.size();
        for (int i = 0; i < length; i++) {
            dataEntry.add(new Entry(i * 2, yDataList.get(i)));
        }
        LineDataSet dataSets;
        if (mLineChart.getData() != null && mLineChart.getData().getDataSetCount() > 0) {
            dataSets = (LineDataSet) mLineChart.getData().getDataSetByIndex(0);
            dataSets.setValues(dataEntry);
            dataSets.setMode(LineDataSet.Mode.LINEAR);
            dataSets.setDrawCircles(false);//不显示每个节点的圆心
            mLineChart.getData().notifyDataChanged();
            mLineChart.notifyDataSetChanged();
        } else {
            dataSets = new LineDataSet(dataEntry, "");
            LineData lineData = new LineData(dataSets);
            mLineChart.setData(lineData);
        }
    }

    public void add(List<Float> xDataList, List<Float> yDataList) {
        List<Entry> dataEntry = new ArrayList<>();
        int length = xDataList.size() > yDataList.size() ? yDataList.size() : xDataList.size();
        for (int i = 0; i < length; i++) {
            dataEntry.add(new Entry(xDataList.get(i), yDataList.get(i)));
        }
        LineDataSet dataSets;
        if (mLineChart.getData() != null && mLineChart.getData().getDataSetCount() > 0) {
            dataSets = (LineDataSet) mLineChart.getData().getDataSetByIndex(0);
            dataSets.setValues(dataEntry);
            dataSets.setMode(LineDataSet.Mode.LINEAR);
            dataSets.setDrawCircles(false);
            mLineChart.getData().notifyDataChanged();
            mLineChart.notifyDataSetChanged();
        } else {
            dataSets = new LineDataSet(dataEntry, "");
            LineData lineData = new LineData(dataSets);
            mLineChart.setData(lineData);
        }
    }

    public void invalidate() {
        mLineChart.invalidate();
    }
}
